package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrder;
import cc.yugu.yhdw.common.domain.hdw.Stats;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.hdw.ExchangeOrderService;
import cc.yugu.yhdw.webserver.service.hdw.ReportService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

@Controller
@RequestMapping(value = "/security/hdw/report")
public class ReportController extends SecurityController {

    @Autowired
    ExchangeOrderService exchangeOrderService;
    @Autowired
    ReportService reportService;

    @RequestMapping(value = "agent_day_index.htm")
    public void agent_day_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_07_01.getValue());
        model.addAttribute("flag", Stats.Flag.AGENT_DAY.getValue());
    }

    @RequestMapping(value = "agent_month_index.htm")
    public void agent_month_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_07_02.getValue());
        model.addAttribute("flag", Stats.Flag.AGENT_MONTH.getValue());
    }

    @RequestMapping(value = "agency_day_index.htm")
    public void agency_day_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_07_03.getValue());
        model.addAttribute("flag", Stats.Flag.AGENCY_DAY.getValue());
    }

    @RequestMapping(value = "agency_month_index.htm")
    public void agency_month_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_07_04.getValue());
        model.addAttribute("flag", Stats.Flag.AGENCY_MONTH.getValue());
    }

    @RequestMapping(value = "cabinet_day_index.htm")
    public void cabinet_day_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_07_05.getValue());
        model.addAttribute("flag", Stats.Flag.CABINET_DAY.getValue());
    }

    @RequestMapping(value = "cabinet_month_index.htm")
    public void cabinet_month_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_07_06.getValue());
        model.addAttribute("flag", Stats.Flag.CABINET_MONTH.getValue());
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(Stats stats, HttpServletRequest request) {
        if ((stats.getAgentId() == null || stats.getAgentId() == 0) && getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            stats.setAgentId(getSessionUser(request).getAgentId());
        } else if ((stats.getAgencyId() == null || stats.getAgencyId() == 0) && getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            stats.setAgencyId(getSessionUser(request).getAgencyId());
        }
        if ("".equals(stats.getCabinetId()) || "0".equals(stats.getCabinetId())) {
            stats.setCabinetId(null);
        }
//        if (stats.getStatsDate() == null || "".equals(stats.getStatsDate())) {
//            Calendar calendar = Calendar.getInstance();
//            if (stats.getFlag() == Stats.Flag.AGENT_DAY.getValue() || stats.getFlag() == Stats.Flag.AGENCY_DAY.getValue() || stats.getFlag() == Stats.Flag.CABINET_DAY.getValue()) {
//                stats.setStatsDate(calendar.get(Calendar.YEAR) + "-" + String.format("%2s", String.valueOf(calendar.get(Calendar.MONTH) + 1)).replaceAll(" ", "0") + "-" + String.format("%2s", String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))).replaceAll(" ", "0"));
//            } else {
//                stats.setStatsDate(calendar.get(Calendar.YEAR) + "-" + String.format("%2s", String.valueOf(calendar.get(Calendar.MONTH) + 1)).replaceAll(" ", "0"));
//            }
//        }
        return PageResult.successResult(reportService.findPagePaid(stats));
    }
}
